package com.lgkj.ui.manager;

import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.lgkj.R;
import com.lgkj.ui.BaseActivity;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.SharedPreferencesUtils;
import com.lgkj.view.CountDownTimerButton;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/2 0002.
 */

public class BangdingShopActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.cb_pwd)
    CheckBox cbPwd;
    @BindView(R.id.et_ver)
    EditText etVer;
    @BindView(R.id.btn_ver)
    CountDownTimerButton btnVer;

    @Override
    public int getLayoutId() {
        return R.layout.activity_banding_shop;
    }

    @Override
    public void initView() {
        setTitle("店铺绑定");
    }

    @Override
    public void initData() {

    }


    private void bangding() {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(BangdingShopActivity.this));
        map.put("username", etName.getText().toString());
        map.put("password", etPassword.getText().toString());
        map.put("phone", etPhone.getText().toString());
        map.put("verify", etVer.getText().toString());

        HttpUtils.postHttp(map, CommonConstant.bangding, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                Log.i(TAG, "successHttp: " + jsonObject.toString());
                toastLong(jsonObject.optString("msg"), 0);
                if (0 == jsonObject.optInt("code")) {
                    finish();
                    String shop_id = jsonObject.optJSONObject("data").optString("shop_id");
                    SharedPreferencesUtils.setParam(BangdingShopActivity.this, "shop_id", shop_id);
                    String shop_table = jsonObject.optJSONObject("data").optString("shop_table");
                    SharedPreferencesUtils.setParam(BangdingShopActivity.this, "shop_table", shop_table);
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }


    @OnClick({R.id.btn_ver, R.id.btn_save})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_ver:
                Map<String, String> map = new HashMap<>();
                map.put("access_token", CommonUtils.getToken(BangdingShopActivity.this));
                map.put("mobile", etPhone.getText().toString());
                HttpUtils.postHttp(map, CommonConstant.getver, new HttpUtils.HttpCallBack() {
                    @Override
                    public void successHttp(JSONObject jsonObject, int id) {
                        if (0 == jsonObject.optInt("code")) {
                            toastLong("短信验证码发送成功", 0);
                            btnVer.start();
                        } else {
                            toastLong(jsonObject.optString("msg"), 0);
                        }
                    }

                    @Override
                    public void errorHttp(String msg) {

                    }
                });
                break;
            case R.id.btn_save:
                bangding();
                break;
        }
    }
}
